package com.good.jzhouse.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.good.jzhouse.bean.PayRecord;
import com.good.jzhouse.bean.Users;
import com.good.jzhouse.dao.GolerecordDao;
import com.good.jzhouse.dao.PayRecordDao;
import com.good.jzhouse.dao.UsersDao;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/recharge")
public class RechargeController {
    @Resource
    PayRecordDao payRecordDao;
    @Resource
    UsersDao usersDao;
    @Resource
    GolerecordDao golerecordDao;

    @RequestMapping("/rhg")
    @ResponseBody
    public Boolean recharge(PayRecord payRecord, HttpSession session,Integer buygold){
        String ip = (String)session.getAttribute("ip");
        payRecord.setRechargeIp(ip);
        Date d = new Date();
        Timestamp t = new Timestamp(d.getTime());
        payRecord.setRechargeTime(t);
        Users u = (Users)session.getAttribute("user");
        payRecord.setUser(u);
        if (payRecordDao.insert(payRecord)>0) {
            u.setBalance(u.getBalance()-payRecord.getPaymentBalance());
            /*double a=u.getGold()+(payRecord.getPaymentBalance()*10);
            int b=(int)a;*/
            u.setGold(u.getGold()+buygold);
            usersDao.update(u);
            session.setAttribute("user",u);
            return true;
        }
        return  false;
    }


    @RequestMapping("/rhg2")
    @ResponseBody
    public Boolean recharge2(PayRecord payRecord, HttpSession session,Integer buypoint){
        String ip = (String)session.getAttribute("ip");
        payRecord.setRechargeIp(ip);
        Date d = new Date();
        Timestamp t = new Timestamp(d.getTime());
        payRecord.setRechargeTime(t);
        Users u = (Users)session.getAttribute("user");
        payRecord.setUser(u);
        if (payRecordDao.insert(payRecord)>0) {
            u.setBalance(u.getBalance()-payRecord.getPaymentBalance());
            /*double a=u.getGold()+(payRecord.getPaymentBalance()*10);
            int b=(int)a;*/
            u.setIntegral(u.getIntegral()+buypoint);
            usersDao.update(u);
            session.setAttribute("user",u);
            return true;
        }
        return  false;
    }


    @RequestMapping("/list1")
    @ResponseBody
    public String list1(HttpSession session,Integer page,Integer limit){
        if (limit == null || limit==0)limit = 1;
        if (page == null)page = 1;
        Users user = (Users)session.getAttribute("user");
        Map<String,Object> map = new HashMap<>();
        if(user.getRole().getRoleid()==1){
            map.put("data", JSONObject.toJSON(payRecordDao.findAll((page-1)*limit,limit)));
            map.put("count",payRecordDao.count());
        }else if(user.getRole().getRoleid()==2){
            map.put("data", JSONObject.toJSON(payRecordDao.findAllbyid(user.getUid(),(page-1)*limit,limit)));
            map.put("count",payRecordDao.countbyid(user.getUid()));
        }
        map.put("code",0);
        map.put("msg","成功");
        return JSON.toJSONStringWithDateFormat(map,"yyyy-MM-dd");
    }

    @RequestMapping("/list2")
    @ResponseBody
    public String list2(HttpSession session,Integer page,Integer limit){
        if (limit == null || limit==0)limit = 1;
        if (page == null)page = 1;
        Users user = (Users)session.getAttribute("user");
        Map<String,Object> map = new HashMap<>();
        if(user.getRole().getRoleid()==1){
            map.put("data", JSONObject.toJSON(golerecordDao.findAlls((page-1)*limit,limit)));
            map.put("count",golerecordDao.count());
        }else if(user.getRole().getRoleid()==2){
            map.put("data", JSONObject.toJSON(golerecordDao.findAllbyid(user.getUid(),(page-1)*limit,limit)));
            map.put("count",golerecordDao.countbyid(user.getUid()));
        }
        map.put("code",0);
        map.put("msg","成功");
        return JSON.toJSONStringWithDateFormat(map,"yyyy-MM-dd");
    }

    @RequestMapping("/t")
    @ResponseBody
    public boolean updategold(HttpSession session,Integer gold){
        Users u = (Users)session.getAttribute("user");
        u.setBalance(u.getBalance()-gold);
        u.setGold(u.getGold()+gold);
        return usersDao.updategolds(u.getGold(),u.getBalance(),u.getUid())>0;
    }










}
